/*
 ============================================================================
 Name		: TetrisGameAppView.h
 Author	  : Vladimir Hozjan
 Copyright   : Copyright (C) 2008 Vladimir Hozjan <vladimir.hozjan@gmail.com>
 Description : Declares view class for application.
 ============================================================================
 */

#ifndef __TETRISGAMEAPPVIEW_h__
#define __TETRISGAMEAPPVIEW_h__

// INCLUDES
#include <coecntrl.h>
#include <bitstd.h>
#include <bitdev.h>

// FORWARD CLASS DECLARATION
class Tetris;

// CLASS DECLARATION
class CTetrisGameAppView : public CCoeControl
	{
public:
	// New methods

	/**
	 * NewL.
	 * Two-phased constructor.
	 * Create a CTetrisGameAppView object, which will draw itself to aRect.
	 * @param aRect The rectangle this view will be drawn to.
	 * @return a pointer to the created instance of CTetrisGameAppView.
	 */
	static CTetrisGameAppView* NewL(const TRect& aRect);

	/**
	 * NewLC.
	 * Two-phased constructor.
	 * Create a CTetrisGameAppView object, which will draw itself
	 * to aRect.
	 * @param aRect Rectangle this view will be drawn to.
	 * @return A pointer to the created instance of CTetrisGameAppView.
	 */
	static CTetrisGameAppView* NewLC(const TRect& aRect);

	/**
	 * ~CTetrisGameAppView
	 * Virtual Destructor.
	 */
	virtual ~CTetrisGameAppView();

public:
	// Functions from base classes

	/**
	 * From CCoeControl, Draw
	 * Draw this CTetrisGameAppView to the screen.
	 * @param aRect the rectangle of this view that needs updating
	 */
	void Draw(const TRect& aRect) const;

	/**
	 * From CoeControl, SizeChanged.
	 * Called by framework when the view size is changed.
	 */
	virtual void SizeChanged();

	/**
	 * From CoeControl, HandlePointerEventL.
	 * Called by framework when a pointer touch event occurs.
	 * Note: although this method is compatible with earlier SDKs, 
	 * it will not be called in SDKs without Touch support.
	 * @param aPointerEvent the information about this event
	 */
	virtual void HandlePointerEventL(const TPointerEvent& aPointerEvent);

    /**
    * OfferKeyEventL
    * From CCoeControl, Handles key event.
    * @param aKeyEvent The key event.
    * @param aType The type of the event.
    * @return Indicates whether or not the key event was
    *           used by this control.
    */
    TKeyResponse OfferKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType );


private:
	// Constructors

	/**
	 * ConstructL
	 * 2nd phase constructor.
	 * Perform the second phase construction of a
	 * CTetrisGameAppView object.
	 * @param aRect The rectangle this view will be drawn to.
	 */
	void ConstructL(const TRect& aRect);

	/**
	 * CTetrisGameAppView.
	 * C++ default constructor.
	 */
	CTetrisGameAppView();

	// for Double buffering
public:
	void DrawToBackBuffer();


    void setTetris(Tetris *aTetris)
    {
        this->iTetris = aTetris;
    }

    // for Double buffering
private:
	void CreateBackBufferL();
	void ReleaseBackBuffer();
	
	void drawRect(TInt x, TInt y, TUint16 color);
	void drawRectNext(TInt x, TInt y, TUint16 color);

	// for Double buffering
private:
	CFbsBitmap*                     iBackBuffer;
	CFbsBitmapDevice*               iBackBufferDevice;
	CFbsBitGc*                      iBackBufferContext;
	TSize                           iBackBufferSize;
        
	const TInt 						iMaxRow;
	const TInt 						iMaxCol;
	Tetris*							iTetris;
	TInt iBlockSize;
    TInt iEmptySpace;

	};

#endif // __TETRISGAMEAPPVIEW_h__
// End of File
